load("@grpc_ecosystem_grpc_gateway//protoc-gen-openapiv2:defs.bzl", "protoc_gen_openapiv2")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

package(default_visibility = ["//visibility:public"])

# TODO(yannic): Add examples/tests that use import_prefix/strip_import_prefix.

# gazelle:exclude a_bit_of_everything.pb.gw.go
# gazelle:exclude a_bit_of_everything_grpc.pb.go
# gazelle:exclude echo_service.pb.gw.go
# gazelle:exclude echo_service_grpc.pb.go
# gazelle:exclude enum_with_single_value.pb.gw.go
# gazelle:exclude enum_with_single_value.pb.go
# gazelle:exclude excess_body.pb.gw.go
# gazelle:exclude excess_body_grpc.pb.go
# gazelle:exclude flow_combination.pb.gw.go
# gazelle:exclude flow_combination_grpc.pb.go
# gazelle:exclude generate_unbound_methods.pb.gw.go
# gazelle:exclude generate_unbound_methods_grpc.pb.go
# gazelle:exclude generated_input.proto
# gazelle:exclude non_standard_names.pb.gw.go
# gazelle:exclude non_standard_names_grpc.pb.go
# gazelle:exclude response_body_service.pb.gw.go
# gazelle:exclude response_body_service_grpc.pb.go
# gazelle:exclude stream.pb.gw.go
# gazelle:exclude stream_grpc.pb.go
# gazelle:exclude use_go_template.pb.gw.go
# gazelle:exclude use_go_template_grpc.pb.go
# gazelle:exclude ignore_comment.pb.gw.go
# gazelle:exclude remove_internal_comment.pb.gw.go
# gazelle:exclude ignore_comment_grpc.pb.go
# gazelle:exclude wrappers.pb.gw.go
# gazelle:exclude wrappers_grpc.pb.go
# gazelle:exclude unannotated_echo_service.pb.gw.go
# gazelle:exclude unannotated_echo_service_grpc.pb.go
# gazelle:exclude visibility_rule_echo_service.pb.gw.go
# gazelle:exclude visibility_rule_echo_service_grpc.pb.go
# gazelle:exclude opaque.proto
# gazelle:exclude opaque.pb.go
# gazelle:exclude opaque.pb.gw.go
# gazelle:exclude opaque_grpc.pb.go
# gazelle:exclude openapi_merge_a.proto
# gazelle:exclude openapi_merge_b.proto
# gazelle:go_grpc_compilers //:go_apiv2, //:go_grpc, //protoc-gen-grpc-gateway:go_gen_grpc_gateway

genrule(
    name = "generated_proto",
    srcs = ["generated_input.proto"],
    outs = ["generated_output.proto"],
    cmd = "cp $< $@",  # A simple copy simulates a generated proto file.
)

proto_library(
    name = "examplepb_proto",
    srcs = [
        "a_bit_of_everything.proto",
        "echo_service.proto",
        "enum_with_single_value.proto",
        "excess_body.proto",
        "flow_combination.proto",
        "generate_unbound_methods.proto",
        "generated_output.proto",
        "ignore_comment.proto",
        "non_standard_names.proto",
        "remove_internal_comment.proto",
        "response_body_service.proto",
        "stream.proto",
        "unannotated_echo_service.proto",
        "use_go_template.proto",
        "visibility_rule_echo_service.proto",
        "wrappers.proto",
    ],
    deps = [
        "//examples/internal/proto/oneofenum:oneofenum_proto",
        "//examples/internal/proto/pathenum:pathenum_proto",
        "//examples/internal/proto/sub:sub_proto",
        "//examples/internal/proto/sub2:sub2_proto",
        "//protoc-gen-openapiv2/options:options_proto",
        "@com_google_protobuf//:duration_proto",
        "@com_google_protobuf//:empty_proto",
        "@com_google_protobuf//:field_mask_proto",
        "@com_google_protobuf//:struct_proto",
        "@com_google_protobuf//:timestamp_proto",
        "@com_google_protobuf//:wrappers_proto",
        "@googleapis//google/api:annotations_proto",
        "@googleapis//google/api:field_behavior_proto",
        "@googleapis//google/api:httpbody_proto",
        "@googleapis//google/api:visibility_proto",
        "@googleapis//google/rpc:status_proto",
    ],
)

#keep
proto_library(
    name = "examplepb_opaque_proto",
    srcs = [
        "opaque.proto",
    ],
    deps = [
        "@com_google_protobuf//:duration_proto",
        "@com_google_protobuf//:field_mask_proto",
        "@com_google_protobuf//:timestamp_proto",
        "@com_google_protobuf//:wrappers_proto",
        "@googleapis//google/api:annotations_proto",
    ],
)

#keep
proto_library(
    name = "openapi_merge_proto",
    srcs = [
        "openapi_merge_a.proto",
        "openapi_merge_b.proto",
    ],
    deps = [
        "@com_google_protobuf//:api_proto",
        "@googleapis//google/api:annotations_proto",
        "@googleapis//google/api:field_behavior_proto",
        "@googleapis//google/rpc:status_proto",
    ],
)

#keep
go_proto_library(
    name = "examplepb_go_proto",
    compilers = [
        "//:go_apiv2",
        "//:go_grpc",
        "//protoc-gen-grpc-gateway:go_gen_grpc_gateway",
    ],
    importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb",
    proto = ":examplepb_proto",
    deps = [
        "//examples/internal/proto/oneofenum",
        "//examples/internal/proto/pathenum",
        "//examples/internal/proto/sub",
        "//examples/internal/proto/sub2",
        "//protoc-gen-openapiv2/options",
        "@com_github_golang_protobuf//descriptor:go_default_library_gen",  # keep
        "@org_golang_google_genproto_googleapis_api//annotations",
        "@org_golang_google_genproto_googleapis_api//httpbody",
        "@org_golang_google_genproto_googleapis_api//visibility",
        "@org_golang_google_genproto_googleapis_rpc//status",
    ],
)

#keep
go_proto_library(
    name = "examplepb_opaque_go_proto",
    compilers = [
        "//:go_apiv2_opaque",
        "//:go_grpc",
        "//protoc-gen-grpc-gateway:go_gen_grpc_gateway_opaque",
    ],
    importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb",
    proto = ":examplepb_opaque_proto",
    deps = [
        "@com_github_golang_protobuf//descriptor:go_default_library_gen",
        "@org_golang_google_genproto_googleapis_api//annotations",
    ],
)

go_library(
    name = "examplepb",
    srcs = [
        "openapi_merge_a.pb.go",
        "openapi_merge_a.pb.gw.go",
        "openapi_merge_a_grpc.pb.go",
        "openapi_merge_b.pb.go",
        "openapi_merge_b.pb.gw.go",
        "openapi_merge_b_grpc.pb.go",
    ],
    embed = [
        ":examplepb_go_proto",
        ":examplepb_opaque_go_proto",  #keep
    ],
    importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb",
    deps = [
        "//runtime",
        "//utilities",
        "@org_golang_google_genproto_googleapis_api//annotations",
        "@org_golang_google_grpc//:grpc",
        "@org_golang_google_grpc//codes",
        "@org_golang_google_grpc//grpclog",
        "@org_golang_google_grpc//metadata",
        "@org_golang_google_grpc//status",
        "@org_golang_google_protobuf//proto",
        "@org_golang_google_protobuf//reflect/protoreflect",
        "@org_golang_google_protobuf//runtime/protoimpl",
    ],
)

protoc_gen_openapiv2(
    name = "examplepb_protoc_gen_openapiv2",
    proto = ":examplepb_proto",
)

protoc_gen_openapiv2(
    name = "examplepb_protoc_gen_openapiv2_merged",
    proto = ":examplepb_proto",
    single_output = True,  # Outputs a single swagger.json file.
)

protoc_gen_openapiv2(
    name = "examplepb_openapi_merge",
    proto = ":openapi_merge_proto",
    single_output = True,  # Outputs a single swagger.json file.
)

alias(
    name = "go_default_library",
    actual = ":examplepb",
    visibility = ["//examples:__subpackages__"],
)
